---@diagnostic disable: undefined-global

-- 添加poxi方法用于选择两张花色相同的手牌当作万箭齐发
Fk:addPoxiMethod{
  name = "zhilie_archery",
  card_filter = function(to_select, selected, data, extra_data)
    if #selected >= 2 then return end -- 最多选2张
    local card = Fk:getCardById(to_select)
    local suit = card.suit
    if #selected == 0 then return true end
    return suit == Fk:getCardById(selected[1]).suit
  end,
  feasible = function(selected, data, extra_data)
    return #selected == 2 -- 必须选2张
  end,
  prompt = function(data, extra_data)
    return "直烈：选择两张花色相同的手牌当作【万箭齐发】使用"
  end,
  default_choice = function(data, extra_data)
    -- 默认选择策略：找到第一组花色相同的两张牌
    local cards = data[1][2] -- 手牌列表
    local suit_groups = {}
    
    for _, card_id in ipairs(cards) do
      local card = Fk:getCardById(card_id)
      local suit = card:getSuitString()
      if not suit_groups[suit] then
        suit_groups[suit] = {}
      end
      table.insert(suit_groups[suit], card_id)
      if #suit_groups[suit] == 2 then
        return suit_groups[suit] -- 返回第一组有两张相同花色的牌
      end
    end
    
    return {} -- 找不到则返回空
  end
}

--- 直烈技能
--- 阶段技，你可以令一名角色摸一张牌，然后若其有相同花色的手牌，其发动"乱击"，否则你需令一名角色对你发动"强袭"。
local zhilie = fk.CreateSkill{
  name = "lvshi__zhilie",
  frequency = "active",
}

zhilie:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#zhilie-active",
  max_phase_use_time = 1,
  target_num = 1,
  target_filter = Util.TrueFunc,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    if not target or target.dead or player.dead then return end
    -- 目标角色摸一张牌
    room:drawCards(target, 1, zhilie.name)
    -- 检查是否有相同花色的手牌
    local hasSameSuit = false
    local handcards = target:getCardIds("h")
    if #handcards > 1 then
      local suits = {}
      for _, cardId in ipairs(handcards) do
        local card = Fk:getCardById(cardId)
        local suit = card:getSuitString()
        if suits[suit] then
          hasSameSuit = true
          break
        else
          suits[suit] = true
        end
      end
    end
    if hasSameSuit then
      -- 发动"乱击" - 选择两张花色相同的手牌当作万箭齐发使用
      local handcards = target:getCardIds("h")
      if #handcards >= 2 then
        local cards_data = {{"$Hand", handcards}}
        local selected_cards = room:askToPoxi(target, {
          poxi_type = "zhilie_archery",
          data = cards_data,
          extra_data = {
            skillName = zhilie.name,
            prompt = "直烈：选择两张花色相同的手牌当作【万箭齐发】使用"
          },
          cancelable = false
        })
        
        if #selected_cards == 2 then
          -- 使用选中的牌作为子牌，创建万箭齐发
          room:useVirtualCard("archery_attack", selected_cards, target, room:getOtherPlayers(target), "luanji")
        end
      end
    else
      -- 先选择一名角色，再让这名角色选择是否使用武器牌对玩家发动强袭
      local chosen_targets = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#zhilie-choose-attacker",
      skill_name = zhilie.name,
    })
      if #chosen_targets > 0 then
        local attacker = chosen_targets[1]
        
        -- 让攻击者选择是否使用武器牌（可以不选）
        local weapon_cards = room:askToCards(attacker, {
          include_equip = true,
          min_num = 1,
          max_num = 1,
          prompt = "#zhilie-attacker-choose-weapon",
          skill_name = zhilie.name,
          pattern = ".|.|.|.|.|weapon",
          cancelable = true,
        })
        
        room:notifySkillInvoked(attacker, "qiangxi", "offensive", {player})
        
        -- 根据选择执行效果
        if #weapon_cards > 0 then
          -- 弃置武器牌
          room:throwCard(weapon_cards, "qiangxi", attacker, attacker)
        else
          -- 失去1点体力
          room:loseHp(attacker, 1, "qiangxi")
        end
        
        -- 对玩家造成1点伤害
        if not player.dead then
          room:damage({
            from = attacker,
            to = player,
            damage = 1,
            skillName = "qiangxi",
          })
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["lvshi__zhilie"] = "直烈",
  [":lvshi__zhilie"] = "阶段技，你可以令一名角色摸一张牌，然后若其有相同花色的手牌，其发动”乱击“，否则你需令一名角色对你发动”强袭“。",
  
  ["#zhilie-active"] = "直烈：选择一名角色，其摸一张牌",
  ["#zhilie-choose-attacker"] = "直烈：选择一名角色对你发动强袭",
  ["#zhilie-attacker-choose-weapon"] = "强袭：选择一张武器牌（不选则失去1点体力）",
  ["luanji"] = "乱击",
  ["qiangxi"] = "强袭",
  
  ["$lvshi__zhilie1"] = "直言进谏，虽死不避！",
  ["$lvshi__zhilie2"] = "烈性如火，宁折不弯！",
}

return zhilie